package com.cinsea.crm.ext;

import com.cinsea.common.Env;
import com.cinsea.common.JdbcDao;
import com.cinsea.htmlselect.service.OptionService;
import com.cinsea.interfaces.model.Cell;
import com.cinsea.interfaces.model.Dataset;
import com.cinsea.interfaces.workflow.RequestInfo;
import com.cinsea.interfaces.workflow.WorkflowServiceImpl;
import com.cinsea.timer.BaseJob;
import com.cinsea.util.DateUtil;
import com.cinsea.util.StringUtil;
import org.quartz.JobExecutionContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ContactNotify extends BaseJob{
    JdbcDao jdbcDao = Env.getBean("jdbcDao");
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        String sql = "";
        //1.查询满足条件的客户信息：条件：a.下次联系日期-1=当前日期的客户

        String nextDate = DateUtil.dayMove(DateUtil.getCurrentDate(),1);
        sql = "select crmid,lxer,xclxrq from ut_contactlist where xclxrq=?";
        List<Map> crmidList =  jdbcDao.queryForList(sql, new Object[]{nextDate});
        if(!(crmidList.size()>0)){
            return;
        }
        List<Map> custList = new ArrayList<Map>();
        for(Map map : crmidList){
            String crmid = StringUtil.null2String(map.get("crmid")  );
            sql="select name,crmtype from ut_customerinfo where processid=?";
            Map info = jdbcDao.queryForMap(sql,new Object[]{crmid});
            map.putAll(info);
            custList.add(map);
        }
        //2.创建通知流程
        List processidList = createNotify(custList);

    }

    /**
     * 创建通知流程
     */
    private List createNotify(List<Map> custList){
        OptionService optionService =  Env.getBean("optionService");
        WorkflowServiceImpl si = new WorkflowServiceImpl();
        List resultList = new ArrayList();
        String processId="";
        for(Map info : custList){
            String sql = "select count(*) as count from ut_liuyan where title = '客户联系提醒' and liuyanren='11111111111111111111111111111111' and xgkh=?";
            Map countMap = jdbcDao.queryForMap(sql,new Object[]{StringUtil.null2String(info.get("crmid"))});
            int count = Integer.parseInt(StringUtil.null2String(countMap.get("count")));
            if(count > 0){
                continue;
            }
            RequestInfo formdata = new RequestInfo();
            formdata.setTypeid("402882e42d5a8fdc012d5b39ba110047");//通知流程ID
            formdata.setOperator("11111111111111111111111111111111");//系统管理员
            formdata.setTitle("客户联系提醒");
            Dataset dataset = new Dataset();
            List<Cell> mainlist = new ArrayList(); //主表单元格
            String currentdate=DateUtil.getCurrentDate();
            Cell c1 = new Cell();//title
            c1.setId("ut_liuyan_title");
            c1.setValue("客户联系提醒");
            Cell c2 = new Cell();//留言发送人
            c2.setId("ut_liuyan_liuyanren");
            c2.setValue("11111111111111111111111111111111");
            Cell c3 = new Cell();//留言日期
            c3.setId("ut_liuyan_liuyanriqi");
            c3.setValue(DateUtil.getCurrentDate());
            Cell c4 = new Cell();//被留言人  || 接收人
            c4.setId("ut_liuyan_blyr");
            c4.setValue(StringUtil.null2String(info.get("lxer")));
            Cell c5 = new Cell();//留言内容
            c5.setId("ut_liuyan_content");
            c5.setValue("您于"+info.get("xclxrq")+"日有重要客户 "+StringUtil.null2String(info.get("name"))+" 需要进行联系！请做好安排。");
            Cell c6 = new Cell();//相关客户
            c6.setId("ut_liuyan_xgkh");
            c6.setValue(StringUtil.null2String(info.get("crmid")));
            mainlist.add(c1);
            mainlist.add(c2);
            mainlist.add(c3);
            mainlist.add(c4);
            mainlist.add(c5);
            mainlist.add(c6);
            dataset.setMaintable(mainlist);
            formdata.setData(dataset);
            processId = si.createRequest(formdata);
            resultList.add(processId);
        }
        return resultList;
    }
}
